﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace PeisongWeb.admin
{
    public partial class productAutoAddList : adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("CF", 1))
                {
                    parentCategoryBind();
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }

        private void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("1=1");

            if (!string.IsNullOrEmpty(ddl_CategorySub.SelectedValue) && ddl_CategorySub.SelectedValue != "-1")
                where.AppendFormat(" and categoryId={0}",ddl_CategorySub.SelectedValue);
            if (!string.IsNullOrEmpty(txt_ProductName.Text))
                where.AppendFormat(" and [name] like '%{0}%'",txt_ProductName.Text);
            if (!string.IsNullOrEmpty(txt_ProductNo.Text))
                where.AppendFormat(" and productNo='{0}'",txt_ProductNo.Text);

            DataSet ds = logic.product.listWithAutoAddPrice(where.ToString(), "productId,sellerPrice", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }

        private void parentCategoryBind()
        {
            DataTable dt=logic.category.getByParent(0);
            ddl_CategoryRoot.DataSource = dt;
            ddl_CategoryRoot.DataBind();
            ddl_CategoryRoot.Items.Insert(0,new ListItem("-请选择-","-1"));
        }
        protected void ddl_CategoryRoot_SelectedIndexChanged(object sender,EventArgs e)
        {
            if (ddl_CategoryRoot.SelectedValue!="-1")
            {
                DataTable dt = logic.category.getByParent(Convert.ToInt32(ddl_CategoryRoot.SelectedValue));
                ddl_CategorySub.DataSource = dt;
                ddl_CategorySub.DataBind();
                ddl_CategorySub.Items.Insert(0,new ListItem("-请选择-","-1"));
            }
        }
        protected void lbtn_Search_Click(object sender, EventArgs e)
        {
            rptBind();
        }

        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName=="Edit")
            {
                int productId = 0;
                int.TryParse(e.CommandArgument.ToString(),out productId);

                decimal autoAddNew = Convert.ToDecimal(hdCurrentNewAutoAdd.Value);
                decimal autoAddOld = Convert.ToDecimal(hdCurrentOldAutoAdd.Value);

                if (autoAddNew!=autoAddOld)
                {
                    logic.product.productUpdateAutoAdd(logic.sysAdmin.AdminID, productId, autoAddNew);

                    Library.Script.ClientMsg("保存成功!");

                    rptBind();
                }
            }
        }
    }
}
